package speed2010.andreza.vspeed.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import speed2010.andreza.vspeed.integration.CommunicationManager;
import speed2010.andreza.vspeed.util.SwingUtils;


/**
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public class Events {

	public static final ActionListener LISTENER = new ActionListener() {

		@Override
		public void actionPerformed(final ActionEvent e) {
			final Object source = e.getSource();
			final boolean isPD = CommunicationManager.getInstance().isPD();
			final boolean isInitialized = CommunicationManager.getInstance().isInitialized();
			final boolean isUsingAnyOntology = CommunicationManager.getInstance().isUsingAnyOntology();

			if (source == GUIConstants.GUI_INSTANCE.jMenuItemExit) {
				GUIConstants.GUI_INSTANCE.dispose();
			} else {
				if (source == GUIConstants.GUI_INSTANCE.jMenuItemVisualQuery) {
					if (!isPD && !isUsingAnyOntology) {
						if (isInitialized) {
							GUIConstants.GUI_INSTANCE.changePanel(new PanelChooseDomain(source));
						}
					} else {
						if ((isPD && !isUsingAnyOntology) || (!isPD && isUsingAnyOntology)) {
							if (isInitialized) {
								GUIConstants.GUI_INSTANCE.changePanel(new PanelVisualQuery());
							}
						}
					}
				} else if (source == GUIConstants.GUI_INSTANCE.jMenuItemSparQL) {
					if (!isPD && !isUsingAnyOntology) {
						if (isInitialized) {
							GUIConstants.GUI_INSTANCE.changePanel(new PanelChooseDomain(source));
						}
					} else if ((isPD && !isUsingAnyOntology) || (!isPD && isUsingAnyOntology)) {
						if (isInitialized) {
							GUIConstants.GUI_INSTANCE.changePanel(new PanelSparQL());
						}
					}
				} else if (source == GUIConstants.GUI_INSTANCE.jMenuItemTheSystem) {
					GUIConstants.GUI_INSTANCE.changePanel(new PanelSystem());
				} else if (source == GUIConstants.GUI_INSTANCE.jMenuItemJavadoc) {
					GUIConstants.GUI_INSTANCE.changePanel(new PanelJavadoc());
				} else if (source == GUIConstants.GUI_INSTANCE.jMenuItemUserGuide) {
					GUIConstants.GUI_INSTANCE.changePanel(new PanelUserguide());
				} else if (source == GUIConstants.GUI_INSTANCE.jMenuItemViewNetwork) {
					GUIConstants.GUI_INSTANCE.changePanel(new PanelViewNetwork());
				} else if (source == GUIConstants.GUI_INSTANCE.jMenuItemJoinNetwork) {
					if (isInitialized) {
						GUIConstants.GUI_INSTANCE.changePanel(new PanelFormNewPeer());
					}
				} else if (source == GUIConstants.GUI_INSTANCE.jMenuItemChoosePeer) {
					if (!isPD) {
						GUIConstants.GUI_INSTANCE.changePanel(new PanelChooseDomain(source));
					} else {
						SwingUtils.infoMessage(new JFrame(), "Option unavaible to participant peer.", "Peer Type Error");
					}
				} else {
					System.out.println(e);
				}
			}
		}
	};

	public static final MouseAdapter MOUSE_LISTENER = new MouseAdapter() {
		@Override
		public void mouseClicked(final MouseEvent e) {

		}
	};
}
